package org.mathcloud.math.core.generic.operator.algebra;

import org.mathcloud.math.core.common.operator.algebra.Inverse;
import org.mathcloud.math.core.common.quantity.ComplexNumber;
import org.mathcloud.math.core.common.quantity.RealNumber;
import org.mathcloud.math.core.generic.operator.GenericUnaryOperator;
import org.mathcloud.math.core.generic.quantity.GenericComplexNumber;
import org.mathcloud.math.core.generic.quantity.GenericRealNumber;

public class GenericInverse extends GenericUnaryOperator implements Inverse
{

	public GenericInverse()
	{
		super();
		signature.setMMLContentTag("inverse");
	}
	
	@Override
	public void calculate()
	{
		if ((getResultSignature()!=null)&&(operand!=null))
		{
			if (operand.getSignature().isRealNumberSignature())
				res = ((RealNumber)operand).getValue()!=0 ?
						new GenericRealNumber(1/((RealNumber)operand).getValue()): null;					
			else if (operand.getSignature().isComplexNumberSignature())
			{
				if (((ComplexNumber)operand).getValue()!=0)
				{
					GenericComplexNumber tRes = new GenericComplexNumber();
					tRes.setMod(1/((ComplexNumber)operand).getMod());					
					tRes.setArg(-((ComplexNumber)operand).getArg());
					res = tRes;
				}
					
					res = new GenericComplexNumber();
			}
		}
	}

}
